/* Copyright (C) 2005-2013, Unigine Corp. All rights reserved.
 *
 * File:    fragment_impostor_sample.h
 * Desc:    Grass impostor sample shader
 * Version: 1.01
 * Author:  Alexander Zapryagaev <frustum@unigine.com>
 *
 * This file is part of the Unigine engine (http://unigine.com/).
 *
 * Your use and or redistribution of this software in source and / or
 * binary form, with or without modification, is subject to: (i) your
 * ongoing acceptance of and compliance with the terms and conditions of
 * the Unigine License Agreement; and (ii) your inclusion of this notice
 * in any version of this software that you use or redistribute.
 * A copy of the Unigine License Agreement is available by contacting
 * Unigine Corp. at http://unigine.com/
 */

/******************************************************************************\
*
* OpenGL
*
\******************************************************************************/

#ifdef OPENGL
	
	half4 diffuse_0 = texture(s_texture_0,s_texcoord_0.xy);
	half4 diffuse_1 = texture(s_texture_0,s_texcoord_0.zy);
	half4 diffuse_2 = texture(s_texture_0,s_texcoord_0.xw);
	half4 diffuse_3 = texture(s_texture_0,s_texcoord_0.zw);
	
	half3 normal_0 = texture2DNormal(s_texture_1,s_texcoord_0.xy);
	half3 normal_1 = texture2DNormal(s_texture_1,s_texcoord_0.zy);
	half3 normal_2 = texture2DNormal(s_texture_1,s_texcoord_0.xw);
	half3 normal_3 = texture2DNormal(s_texture_1,s_texcoord_0.zw);
	
	half4 diffuse_01 = lerp(diffuse_0,diffuse_1,s_texcoord_1.x);
	half4 diffuse_23 = lerp(diffuse_2,diffuse_3,s_texcoord_1.x);
	half4 diffuse = lerp(diffuse_01,diffuse_23,s_texcoord_1.y);
	
	half3 normal_01 = lerp(normal_0,normal_1,s_texcoord_1.x);
	half3 normal_23 = lerp(normal_2,normal_3,s_texcoord_1.x);
	half3 normal = normalize(lerp(normal_01,normal_23,s_texcoord_1.y));
	
	normal.x = -normal.x;
	
	#ifdef NOISE
		half3 noise = texture(s_texture_2,s_texcoord_1.zw).xyz;
		diffuse.xyz = saturate(diffuse.xyz + (noise * 2.0f - 1.0f) * noise_scale);
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
/******************************************************************************\
*
* Direct3D11
*
\******************************************************************************/

#elif DIRECT3D11
	
	half4 diffuse_0 = s_texture_0.Sample(s_sampler_0,IN.texcoord_0.xy);
	half4 diffuse_1 = s_texture_0.Sample(s_sampler_0,IN.texcoord_0.zy);
	half4 diffuse_2 = s_texture_0.Sample(s_sampler_0,IN.texcoord_0.xw);
	half4 diffuse_3 = s_texture_0.Sample(s_sampler_0,IN.texcoord_0.zw);
	
	half3 normal_0 = texture2DNormal(s_texture_1,s_sampler_1,IN.texcoord_0.xy);
	half3 normal_1 = texture2DNormal(s_texture_1,s_sampler_1,IN.texcoord_0.zy);
	half3 normal_2 = texture2DNormal(s_texture_1,s_sampler_1,IN.texcoord_0.xw);
	half3 normal_3 = texture2DNormal(s_texture_1,s_sampler_1,IN.texcoord_0.zw);
	
	half4 diffuse_01 = lerp(diffuse_0,diffuse_1,IN.texcoord_1.x);
	half4 diffuse_23 = lerp(diffuse_2,diffuse_3,IN.texcoord_1.x);
	half4 diffuse = lerp(diffuse_01,diffuse_23,IN.texcoord_1.y);
	
	half3 normal_01 = lerp(normal_0,normal_1,IN.texcoord_1.x);
	half3 normal_23 = lerp(normal_2,normal_3,IN.texcoord_1.x);
	half3 normal = normalize(lerp(normal_01,normal_23,IN.texcoord_1.y));
	
	normal.x = -normal.x;
	
	#ifdef NOISE
		half3 noise = s_texture_2.Sample(s_sampler_2,IN.texcoord_1.zw).xyz;
		diffuse.xyz = saturate(diffuse.xyz + (noise * 2.0f - 1.0f) * noise_scale);
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
/******************************************************************************\
*
* Direct3D9
*
\******************************************************************************/

#elif DIRECT3D9
	
	half4 diffuse_0 = tex2D(s_texture_0,IN.texcoord_0.xy);
	half4 diffuse_1 = tex2D(s_texture_0,IN.texcoord_0.zy);
	half4 diffuse_2 = tex2D(s_texture_0,IN.texcoord_0.xw);
	half4 diffuse_3 = tex2D(s_texture_0,IN.texcoord_0.zw);
	
	half3 normal_0 = tex2DNormal(s_texture_1,IN.texcoord_0.xy);
	half3 normal_1 = tex2DNormal(s_texture_1,IN.texcoord_0.zy);
	half3 normal_2 = tex2DNormal(s_texture_1,IN.texcoord_0.xw);
	half3 normal_3 = tex2DNormal(s_texture_1,IN.texcoord_0.zw);
	
	half4 diffuse_01 = lerp(diffuse_0,diffuse_1,IN.texcoord_1.x);
	half4 diffuse_23 = lerp(diffuse_2,diffuse_3,IN.texcoord_1.x);
	half4 diffuse = lerp(diffuse_01,diffuse_23,IN.texcoord_1.y);
	
	half3 normal_01 = lerp(normal_0,normal_1,IN.texcoord_1.x);
	half3 normal_23 = lerp(normal_2,normal_3,IN.texcoord_1.x);
	half3 normal = normalize(lerp(normal_01,normal_23,IN.texcoord_1.y));
	
	normal.x = -normal.x;
	
	#ifdef NOISE
		half3 noise = tex2D(s_texture_2,IN.texcoord_1.zw).xyz;
		diffuse.xyz = saturate(diffuse.xyz + (noise * 2.0f - 1.0f) * noise_scale);
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
/******************************************************************************\
*
* PlayStation3
*
\******************************************************************************/

#elif PLAYSTATION3
	
	half4 diffuse_0 = h4tex2D(s_texture_0,IN.texcoord_0.xy);
	half4 diffuse_1 = h4tex2D(s_texture_0,IN.texcoord_0.zy);
	half4 diffuse_2 = h4tex2D(s_texture_0,IN.texcoord_0.xw);
	half4 diffuse_3 = h4tex2D(s_texture_0,IN.texcoord_0.zw);
	
	half3 normal_0 = tex2DNormal(s_texture_1,IN.texcoord_0.xy);
	half3 normal_1 = tex2DNormal(s_texture_1,IN.texcoord_0.zy);
	half3 normal_2 = tex2DNormal(s_texture_1,IN.texcoord_0.xw);
	half3 normal_3 = tex2DNormal(s_texture_1,IN.texcoord_0.zw);
	
	half4 diffuse_01 = lerp(diffuse_0,diffuse_1,IN.texcoord_1.x);
	half4 diffuse_23 = lerp(diffuse_2,diffuse_3,IN.texcoord_1.x);
	half4 diffuse = lerp(diffuse_01,diffuse_23,IN.texcoord_1.y);
	
	half3 normal_01 = lerp(normal_0,normal_1,IN.texcoord_1.x);
	half3 normal_23 = lerp(normal_2,normal_3,IN.texcoord_1.x);
	half3 normal = normalize(lerp(normal_01,normal_23,IN.texcoord_1.y));
	
	normal.x = -normal.x;
	
	#ifdef NOISE
		half3 noise = h3tex2D(s_texture_2,IN.texcoord_1.zw);
		diffuse.xyz = saturate(diffuse.xyz + (noise * 2.0f - 1.0f) * noise_scale);
	#endif
	
	#ifdef USE_SRGB
		diffuse.xyz *= diffuse.xyz;
	#endif
	
#endif
